from project.filters import ProjectFilter
from project.models import Project
from project.serializers import ProjectSerializer, ProjectCreateUpdateSerializer, ExportProjectSerializer
from apps.vadmin.op_drf.filters import DataLevelPermissionsFilter
from apps.vadmin.op_drf.viewsets import CustomModelViewSet
from apps.vadmin.permission.permissions import CommonPermission


class ProjectModelViewSet(CustomModelViewSet):
    """
    CRUD视图
    """
    queryset = Project.objects.all()
    serializer_class = ProjectSerializer  # 序列化器
    create_serializer_class = ProjectCreateUpdateSerializer  # 创建/更新时的列化器
    update_serializer_class = ProjectCreateUpdateSerializer  # 创建/更新时的列化器
    filter_class = ProjectFilter  # 过滤器
    extra_filter_backends = [DataLevelPermissionsFilter]  # 数据权限类，不需要可注释掉
    update_extra_permission_classes = (CommonPermission,)  # 判断用户是否有这条数据的权限
    destroy_extra_permission_classes = (CommonPermission,)  # 判断用户是否有这条数据的权限
    create_extra_permission_classes = (CommonPermission,)  # 判断用户是否有这条数据的权限
    search_fields = ('addr',)  # 搜索
    ordering = ['create_datetime']  # 默认排序
    # 导出
    export_field_data = ['序号', '成交日期', '成交价', '成交量', '成交额', '成交地点']  # 导出
    export_serializer_class = ExportProjectSerializer  # 导出序列化器
    # 导入
    import_field_data = {'date': '成交日期', 'price': '成交价', 'num': '成交量', 'money': '成交额', 'addr': '成交地点'}
    import_serializer_class = ExportProjectSerializer